import request from '@/utils/request'
import { getDictionaryDataSelector, updateDictionaryState } from '@/api/systemData/dictionary'
import { synchronousJiaXiao } from '@/api/campus/campus'

import StatusBox from '../renewal/StatusBox'
import RecordsHandleForm from '../insurance/Records/index.vue'
import CloseSupportForm from '../supportClose/Form/index.vue'
import VueGridLayout from 'vue-grid-layout'
import { TrainPieRegionChart, TrainTeacherPieRegionChart } from '@/components/VisualPortal'

import { getTrainPanelThree, getTrainSummary, getTrainPanelFour } from '@/api/campus/train'
export default {
  name: 'training_summary',
  components: { StatusBox, RecordsHandleForm, CloseSupportForm, GridLayout: VueGridLayout.GridLayout, GridItem: VueGridLayout.GridItem, TrainPieRegionChart, TrainTeacherPieRegionChart },
  data() {
    return {
      list: [],
      layout: [],
      listLoading: true,
      trainPublishCount: undefined,
      totalTrainees: undefined,
      totalTraineesLabel: [],
      totalTraineesValue: [],

      totalGraduates: undefined,
      totalGraduatesLabel: [],
      totalGraduatesValue: [],

      threeSeach: {
        brand: 'bell',
        year: new Date().getFullYear(),
        month: new Date().getMonth()
      },
      monthTatol: {},
      searchValue: '1',
      options: [
        {
          value: '1',
          label: '本月'
        },
        {
          value: '2',
          label: '上月'
        },
        {
          value: '3',
          label: '上上月'
        }
      ],
      brandOptions: []
    }
  },
  computed: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
    this.initData()
  },
  methods: {
    initData() {
      getTrainSummary({ year: new Date().getFullYear() }).then(res => {
        this.trainPublishCount = res.data.trainPublishCount
        this.totalTrainees = res.data.totalTrainees.total
        res.data.totalTrainees.byBrand.map(item => {
          this.totalTraineesLabel.push(item.brand)
          this.totalTraineesValue.push(item.count)
        })
        this.totalGraduates = res.data.totalGraduates.total
        res.data.totalGraduates.byBrand.map(item => {
          this.totalGraduatesLabel.push(item.brand)
          this.totalGraduatesValue.push(item.count)
        })
      })
      this.searchPanel()
    },
    searchPanel() {
      const now = new Date()
      const monthOffset = {
        '1': 0, // 本月
        '2': -1, // 上月
        '3': -2 // 上上月
      }[this.searchValue]

      // 创建新日期对象避免污染原始日期
      const targetDate = new Date(now)
      if (now.getMonth() + monthOffset < 0) {
        this.threeSeach.year = this.threeSeach.year - 1
      }
      targetDate.setMonth(now.getMonth() + monthOffset)

      // 返回月份（注意：JavaScript月份0-11，需要+1）
      this.threeSeach.month = targetDate.getMonth() + 1

      getTrainPanelThree(this.threeSeach).then(res => {
        this.monthTatol = res.data
      })
      getTrainPanelFour(this.threeSeach).then(res => {
        this.list = res.data
      })
    },
    search() {
      this.initData()
    },
    reset() {
      this.query = {
        brand: 'bell',
        enCode: undefined,
        area: undefined,
        type: undefined,
        stateMark: undefined,
        openDate: undefined,
        businessName: undefined,
        contact: undefined,
        endDate: undefined,
        inurance: undefined,
        inuranceDate: [],
        pageType: '2'
      }

      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
